﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using PetaPoco;

namespace SDF.Web.UI
{
    public class HtmlHelper
    {
        #region Pager
        public string Pager(long pcount, long pagesize, long page, string link)
        {
            return UrlPager(pagesize, page, pcount, link, 8, "‹ 上一页", "下一页 ›", "« 首页", "末页 »", "当前没有数据", "black");
        }

        public string EnPager(long pcount, long pagesize, long page, string link)
        {
            return UrlPager(pagesize, page, pcount, link, 8, "‹ Prev", "Next ›", "« First", "Last »", "No Record", "black");
        }

        public string AdminPager(long pcount, long pagesize, long page, string link)
        {
            return UrlPager(pagesize, page, pcount, link, 5, "‹ 上一页", "下一页 ›", "« ", " »", "没有数据", "flickr");
        }

        public string Pager<T>(Page<T> rs, string link)
        {
            return Pager(rs.TotalItems, rs.ItemsPerPage, rs.CurrentPage, link);
        }
        public string EnPager<T>(Page<T> rs, string link)
        {
            return EnPager(rs.TotalItems, rs.ItemsPerPage, rs.CurrentPage, link);
        }
        public string AdminPager<T>(Page<T> rs, string link)
        {
            return AdminPager(rs.TotalItems, rs.ItemsPerPage, rs.CurrentPage, link);
        }

        /// <summary>
        /// 返回分页页码,参考源dtcms2.0
        /// </summary>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="linkUrl">链接地址，__id__代表页码</param>
        /// <param name="centSize">中间页码数量</param>
        /// <returns></returns>
        public string UrlPager(long pageSize, long pageIndex, long totalCount, string linkUrl, int centSize, string prevText, string nextText, string firstText, string lastText, string noRecordText, string className)
        {
            //计算页数
            if (totalCount < 1 || pageSize < 1)
            {
                return "";
            }
            long pageCount = totalCount / pageSize;
            if (pageCount < 1)
            {
                return "";
            }
            if (totalCount % pageSize > 0)
            {
                pageCount += 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }

            if (totalCount == 0)
            {
                return "<div style='text-align:center;color:#f00'>" + noRecordText + "</div>";
            }

            StringBuilder sb = new StringBuilder();
            sb.Append("<link type=\"text/css\" rel=\"stylesheet\" href=\"/js/pagination/pagination.css\" />\n");
            sb.AppendFormat("<div style='background:none' class='{0}' id='pagination'>", className);

            string pageId = "__id__";
            string prevBtn = "<a href=\"" + linkUrl.Replace(pageId, (pageIndex - 1).ToString()) + "\">" + prevText + "</a>";
            string nextBtn = "<a href=\"" + linkUrl.Replace(pageId, (pageIndex + 1).ToString()) + "\">" + nextText + "</a>";
            string firstStr = "<a href=\"" + linkUrl.Replace(pageId, "1") + "\">1</a>";
            string lastStr = "<a href=\"" + linkUrl.Replace(pageId, pageCount.ToString()) + "\">" + pageCount.ToString() + "</a>";

            if (pageIndex <= 1)
            {
                prevBtn = "<span class=\"disabled\">" + prevText + "</span>";
            }
            if (pageIndex >= pageCount)
            {
                nextBtn = "<span class=\"disabled\">" + nextText + "</span>";
            }
            if (pageIndex == 1)
            {
                firstStr = "<span class=\"current\">1</span>";
            }
            if (pageIndex == pageCount)
            {
                lastStr = "<span class=\"current\">" + pageCount.ToString() + "</span>";
            }
            long firstNum = pageIndex - (centSize / 2); //中间开始的页码
            if (pageIndex < centSize)
                firstNum = 2;
            long lastNum = pageIndex + centSize - ((centSize / 2) + 1); //中间结束的页码
            if (lastNum >= pageCount)
                lastNum = pageCount - 1;
            sb.Append(prevBtn + firstStr);
            if (pageIndex >= centSize)
            {
                sb.Append("<span>...</span>\n");
            }
            for (long i = firstNum; i <= lastNum; i++)
            {
                if (i == pageIndex)
                {
                    sb.Append("<span class=\"current\">" + i + "</span>");
                }
                else
                {
                    sb.Append("<a href=\"" + linkUrl.Replace(pageId, i.ToString()) + "\">" + i + "</a>");
                }
            }
            if (pageCount - pageIndex > centSize - ((centSize / 2)))
            {
                sb.Append("<span>...</span>");
            }
            sb.Append(lastStr + nextBtn);
            return sb.ToString();
        }
        #endregion

    }
}